package ruifeng.zcj.communcationring.ui.appstart;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.se7en.utils.SystemUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import ruifeng.zcj.communcationring.R;
import ruifeng.zcj.communcationring.base.BaseActivity;
import ruifeng.zcj.communcationring.groble.Constant;
import ruifeng.zcj.communcationring.ui.main.MainActivity;

/**
 * Created by Administrator on 2016/10/8 0008.
 */
public class AppStart extends BaseActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.welcome_img)
    ImageView welcome_img;
    @Bind(R.id.welcome_vp)
    ViewPager viewPager;
    @Bind(R.id.start_app)
    Button start_btn;
    private List<ImageView> imageViewList;
    static SharedPreferences spfs;
    //当前app的版本
    private int iCurVersion;
    //上一个app的版本
    private int iSaveVersion;
    private Handler mHandler = new Handler();
    private Handler mHandler2 = new Handler();



    @Override
    public int getLayoutId() {
        return R.layout.app_start;
    }

    @Override
    public void initView() {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏

        spfs = getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        iCurVersion = SystemUtil.getSystemVersionCode();
        iSaveVersion = SystemUtil.getSharedInt(Constant.VERSION_STRING, -1);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                welcome_img.setVisibility(View.GONE);
                if (iCurVersion > -1 && iCurVersion > iSaveVersion) {
                    //第一次运行或升级后首次运行显示欢迎界面
                    //存放图片集合，用viewpage显示
                    isFirstRun();
                } else {
                    //非第一次运行
                    isNotFirstRum();
                }
            }
        }, 2000);

        //对vp设置监听
        viewPager.setOnPageChangeListener(this);
    }

    /**
     * 第一次运行
     */
    private void isFirstRun() {
        imageViewList = new ArrayList<>();

        addImageView(R.drawable.step1);
        addImageView(R.drawable.step2);
        addImageView(R.drawable.step3);
        addImageView(R.drawable.step4);

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(imageViewList.get(position));
                return imageViewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(imageViewList.get(position));
            }
        });
    }
    /**
     * @param imgID
     */
    private void addImageView(int imgID) {
        ImageView imageView = new ImageView(getApplicationContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(imgID);
        imageViewList.add(imageView);
    }



    /**
     * 跳转到...
     */
    private void redirectTo() {
       /* Intent uploadLog = new Intent(this, LogUploadService.class);
        startService(uploadLog);*/
        //隐藏vp 及按钮
        viewPager.setVisibility(View.GONE);
        start_btn.setVisibility(View.GONE);
        SystemUtil.setSharedInt(Constant.VERSION_STRING, iCurVersion);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @OnClick(R.id.start_app)
    public void onClick() {
        redirectTo();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        showSnackBar("第几页"+position);
        showSnackBar("共有"+imageViewList.size());

    }

    //改变page时的操作
    @Override
    public void onPageSelected(int position) {

    /*    showSnackBar("第几页"+position);
        showSnackBar("共有"+imageViewList.size());*/

        start_btn.setVisibility(View.GONE);
        if (position == imageViewList.size() - 1) {
            start_btn.setVisibility(View.VISIBLE);
            start_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //隐藏vp 及按钮
                    viewPager.setVisibility(View.GONE);
                    start_btn.setVisibility(View.GONE);

                    SystemUtil.setSharedInt(Constant.VERSION_STRING, iCurVersion);

                    isNotFirstRum();
                }
            });
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 非第一次运行
     */
    private void isNotFirstRum() {
        welcome_img.setVisibility(View.VISIBLE);
      //  welcome_img.setBackgroundResource(R.mipmap.welcome_2);

        mHandler2.postDelayed(new Runnable() {
            @Override
            public void run() {
                redirectTo();
            }
        }, 800);
    }
}
